import matplotlib.pyplot as plt
import pro.projection as projection

# 创建图形和坐标轴
fig, ax = plt.subplots(figsize=(8, 6))
ax.set_title('点击交互 - 获取坐标点')
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.grid(True)
ax.set_aspect('equal')

# 定义起止点线段
def line(x1,y1,x2,y2):
    return [x1,y1],[x2,y2]


line_start,line_end = line(1,1,65,89)

ax.plot([line_start[0],line_end[0]],[line_start[1],line_end[1]],color='blue',linestyle='-',marker='x')

# 存储点击点的列表
click_points = []

def onclick(event):
    """
    鼠标点击事件处理函数
    """
    # 检查是否在坐标轴内点击
    if event.inaxes != ax:
        return
    
    # 获取点击坐标
    x, y = event.xdata, event.ydata
    click_points.append((x, y))
    
    # 在点击位置绘制红色圆点
    ax.plot(x, y, 'ro', markersize=8)
    
    # 添加坐标文本标签
    # ax.text(x+0.1, y+0.1, f'({x:.2f}, {y:.2f})', fontsize=10)
    pp = projection.point_to_line_projection([x,y],line_start,line_end)
    ax.plot(pp[0],pp[1],'gX')
    
    # 重绘图形
    fig.canvas.draw()

# 连接点击事件
cid = fig.canvas.mpl_connect('button_press_event', onclick)

plt.show()